/*
Copyright (C) 2008  Helmut Juskewycz
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.juskewycz.dividere.core;

import com.juskewycz.dividere.action.AActionFactory;
import com.juskewycz.dividere.action.IProgressListener;
import com.juskewycz.dividere.action.IProgressObserverable;
import com.juskewycz.dividere.action.split.ISplitListener;
import com.juskewycz.dividere.action.split.ISplitter;
import java.io.File;
import java.util.List;

/**
 * Controller to invoke a split process.
 * 
 * @author Helmut Juskewycz
 * @createdon 27. January 2008
 * @lastmodified 27. January 2008
 * @history
 * 27. January 2008 Finished version 1.0
 */
public class SplitController {

    private long splitSize;
    private String filePath;
    private boolean deleteOriginal;
    private boolean overwriteFiles;

    /**
     * Creates a new <code>SplitController</code>. It also sets certain
     * parameters for the splitting process. So typically for every split a
     * <code>SplitController</code> has to be created.
     * 
     * @param filePath The path of the original (large) file
     * @param splitSize The original file will be splitted in parts with this
     * size.
     * @param deleteOriginal Indicates whether the orginal split file should be
     * deleted or not after the split was successful.
     * @param overwriteFiles Indicates whether exisiting files with the same
     * name of the part files should be overwritten or not.
     */
    public SplitController(String filePath, long splitSize, boolean deleteOriginal, boolean overwriteFiles) {
	this.splitSize = splitSize;
	this.filePath = filePath;
	this.deleteOriginal = deleteOriginal;
	this.overwriteFiles = overwriteFiles;
    }

    /**
     * Starts splitting the specified file. If the <code>ISplitter</code>
     * implements <code>IProgressObserverable</code> the given listeners will
     * be added as observers to the splitter.
     * 
     * @param listeners Progress listeners which want to observe the splitting
     * process.
     */
    public void split(List<IProgressListener> listeners) {

	ISplitter splitter = AActionFactory.getImpl().createSplitter();
	if (splitter instanceof IProgressObserverable) {
	    IProgressObserverable observerable = (IProgressObserverable) splitter;
	    for (IProgressListener iProgressListener : listeners) {
		observerable.addListener(iProgressListener);
	    }
	}

	if (deleteOriginal) {
	    splitter.addListener(new ISplitListener() {

		public void splitBegin(File originalFile) {

		}

		public void splitEnd(File originalFile) {
		    originalFile.delete();
		}

		public void splitPartBegin(int number, File partFile) {

		}

		public void splitPartEnd(int number, File partFile) {

		}
		});
	}

	if (overwriteFiles) {
	    splitter.addListener(new ISplitListener() {

		public void splitBegin(File originalFile) {

		}

		public void splitEnd(File originalFile) {

		}

		public void splitPartBegin(int number, File partFile) {
		    partFile.delete();
		}

		public void splitPartEnd(int number, File partFile) {

		}
		});
	}

	splitter.split(filePath, splitSize);
    }
}
